﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem.NursingEntry
{
    public partial class NursingSummaryHours : Form
    {
        public string Date;
        public string Stime;
        public string Etime;
        /// <summary>
        /// 传入日期 开始时间 结束时间 计算自定义统计
        /// </summary>
        /// <param name="date">日期 yyyy-MM-dd</param>
        /// <param name="stime">开始时间 HH:mm</param>
        /// <param name="etime">结束时间 HH:mm</param>
        public NursingSummaryHours(string date, string stime, string etime)
        {
            InitializeComponent();
            this.Date = date;
            this.Stime = stime;
            this.Etime = etime;
            lblDate.Text = date;
            txtStime.Text = stime;
            txtEtime.Text = etime;
            txtStime.KeyUp += txtTime_KeyUp;
            txtEtime.KeyUp += txtTime_KeyUp;
            CheckTime();
        }
        /// <summary>
        /// 传入时间 修改自定义统计
        /// </summary>
        /// <param name="fullbeginTime">yyyy-MM-dd HH:mm:ss.fff</param>
        public NursingSummaryHours(string fullbeginTime)
        {
            InitializeComponent();
            DateTime fullstime = Convert.ToDateTime(fullbeginTime);
            this.Date = fullstime.ToString("yyyy-MM-dd");
            this.Stime = fullstime.ToString("HH:mm");
            this.Etime = fullstime.AddHours(fullstime.Millisecond - 1).ToString("HH:mm");
            lblDate.Text = this.Date;
            txtStime.Text = this.Stime;
            txtEtime.Text = this.Etime;
            txtStime.KeyUp += txtTime_KeyUp;
            txtEtime.KeyUp += txtTime_KeyUp;
            CheckTime();
        }

        void txtTime_KeyUp(object sender, KeyEventArgs e)
        {
            CheckTime();
        }

        private void CheckTime()
        {
            if (string.IsNullOrEmpty(txtStime.Text)) return;
            if (string.IsNullOrEmpty(txtEtime.Text)) return;
            string timehhmmregex = @"^([01]?\d|2[0-3]):[0-5]?\d$";
            if (!Regex.IsMatch(txtStime.Text, timehhmmregex)) return;
            if (!Regex.IsMatch(txtEtime.Text, timehhmmregex)) return;
            TimeSpan stime = Convert.ToDateTime(txtStime.Text).TimeOfDay;
            TimeSpan etime = Convert.ToDateTime(txtEtime.Text).TimeOfDay;
            lblnextday.Visible = (stime > etime);
        }

        private void BtnConfrim_Click(object sender, EventArgs e)
        {
            txtStime.Text = DateTimeHelper.HHmmssToHHmm(txtStime.Text);
            txtEtime.Text = DateTimeHelper.HHmmssToHHmm(txtEtime.Text);
            if (string.IsNullOrEmpty(txtStime.Text)) return;
            if (string.IsNullOrEmpty(txtEtime.Text)) return;
            this.Date = lblDate.Text;
            this.Stime = txtStime.Text;
            this.Etime = txtEtime.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}